package com.ruoyi.supplier.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.supplier.domain.bo.RefundImageBo;
import com.ruoyi.supplier.domain.vo.RefundImageVo;
import com.ruoyi.supplier.domain.RefundImage;
import com.ruoyi.supplier.mapper.RefundImageMapper;
import com.ruoyi.supplier.service.IRefundImageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 售后单图片记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-18
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class RefundImageServiceImpl implements IRefundImageService {

    private final RefundImageMapper baseMapper;

    /**
     * 查询售后单图片记录
     */
    @Override
    public RefundImageVo queryById(Integer refundImageId){
        return baseMapper.selectVoById(refundImageId);
    }

    /**
     * 查询售后单图片记录列表
     */
    @Override
    public TableDataInfo<RefundImageVo> queryPageList(RefundImageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RefundImage> lqw = buildQueryWrapper(bo);
        Page<RefundImageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询售后单图片记录列表
     */
    @Override
    public List<RefundImageVo> queryList(RefundImageBo bo) {
        LambdaQueryWrapper<RefundImage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RefundImage> buildQueryWrapper(RefundImageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RefundImage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRefundId() != null, RefundImage::getRefundId, bo.getRefundId());
        lqw.eq(bo.getImageId() != null, RefundImage::getImageId, bo.getImageId());
        return lqw;
    }

    /**
     * 新增售后单图片记录
     */
    @Override
    public Boolean insertByBo(RefundImageBo bo) {
        RefundImage add = BeanUtil.toBean(bo, RefundImage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRefundImageId(add.getRefundImageId());
        }
        return flag;
    }

    /**
     * 修改售后单图片记录
     */
    @Override
    public Boolean updateByBo(RefundImageBo bo) {
        RefundImage update = BeanUtil.toBean(bo, RefundImage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RefundImage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除售后单图片记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
